package edu.ynu.dao;
// default package

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import edu.ynu.po.Individul;

/**
 	* A data access object (DAO) providing persistence and search support for individul entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .individul
  * @author MyEclipse Persistence Tools 
 */

public class individulDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(individulDAO.class);
		//property constants
	public static final String SEX = "sex";
	public static final String BIRTHDAY = "birthday";
	public static final String AGE_GROUP = "ageGroup";
	public static final String HOBBY = "hobby";
	public static final String SCHOOL = "school";



    
    public void save(Individul transientInstance) {
        log.debug("saving individul instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
    		session.save(transientInstance);  
    		trans.commit();  
    		session.close(); 
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Individul persistentInstance) {
        log.debug("deleting individul instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
            session.delete(persistentInstance);
            trans.commit();  
    		session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update (Individul indiv) {
		Session session = getSession();  
		Transaction trans = session.beginTransaction();  
		session.update(indiv);  
		trans.commit();  
		session.close(); 
	}
	
	public void saveOrUpdate(Individul indiv) {
		Session session = getSession();  
		Transaction trans = session.beginTransaction();  
		session.saveOrUpdate(indiv);  
		trans.commit();  
		session.close();
	}
    
    public Individul findById( java.lang.String id) {
        log.debug("getting individul instance with id: " + id);
        try {
            Individul instance = (Individul) getSession()
                    .get("edu.ynu.po.Individul", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Individul instance) {
        log.debug("finding individul instance by example");
        try {
            List results = getSession()
                    .createCriteria("Individul")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding individul instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Individul as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findBySex(Object sex
	) {
		return findByProperty(SEX, sex
		);
	}
	
	public List findByBirthday(Object birthday
	) {
		return findByProperty(BIRTHDAY, birthday
		);
	}
	
	public List findByAgeGroup(Object ageGroup
	) {
		return findByProperty(AGE_GROUP, ageGroup
		);
	}
	
	public List findByHobby(Object hobby
	) {
		return findByProperty(HOBBY, hobby
		);
	}
	
	public List findBySchool(Object school
	) {
		return findByProperty(SCHOOL, school
		);
	}
	

	public List findAll() {
		log.debug("finding all individul instances");
		try {
			String queryString = "from Individul";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Individul merge(Individul detachedInstance) {
        log.debug("merging individul instance");
        try {
            Individul result = (Individul) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Individul instance) {
        log.debug("attaching dirty individul instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Individul instance) {
        log.debug("attaching clean individul instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}